package com.gwtjs.icustom.utils;

import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.gwtjs.icustom.controller.install.SetSystemController;
import com.gwtjs.icustom.entity.SSHClientVO;
import com.gwtjs.icustom.pojo.ServerHost;

import net.schmizz.sshj.SSHClient;
import net.schmizz.sshj.common.IOUtils;
import net.schmizz.sshj.connection.channel.direct.Session;
import net.schmizz.sshj.connection.channel.direct.Session.Command;
import net.schmizz.sshj.transport.verification.PromiscuousVerifier;

public class JScpUtil {
	
	private final static Logger logger = LoggerFactory.getLogger(SetSystemController.class);
	
	/**
	 * <h2>execute shell script</h2>
	 * @param host
	 * @param sshCmd
	 * @return
	 * @throws IOException
	 */
	static public SSHClientVO exec(final ServerHost host,final String sshCmd) throws IOException {
		SSHClient ssh = new SSHClient();
		
        ssh.addHostKeyVerifier(new PromiscuousVerifier());
        ssh.connect(host.getHostIp(), host.getHostPort());
        ssh.authPassword(host.getAccount(), host.getPassword());
 
        Session session = ssh.startSession();
        Command cmd = session.exec(sshCmd);
        String cmdStream = IOUtils.readFully(cmd.getInputStream()).toString();
        SSHClientVO po = getSSHClient(ssh);
        po.setCmdStream(cmdStream);
        logger.info("cmd Stream: {}",cmdStream);
        session.close();
        ssh.close();
        return po;
	}
	
	static private SSHClientVO getSSHClient(SSHClient ssh) {
		SSHClientVO po = new SSHClientVO();
		boolean isAuth = ssh.isAuthenticated();
		boolean isConn = ssh.isConnected();
		//logger.info("isAuthenticated={} ,isConnected={} ", isAuth, isConn);
		po.setAuth(isAuth);
		po.setConn(isConn);

		String localAddress = ssh.getLocalAddress().getHostAddress();
		String remoteAddress = ssh.getRemoteAddress().getHostAddress();
		//logger.info("local Address={} ,remote Address={} ", localAddress, remoteAddress);
		po.setLocalAddress(localAddress);
		po.setRemoteAddress(remoteAddress);
		
		return po;
	}
	
	static public SSHClientVO upload(ServerHost host,final String local,final String remote) throws IOException {
		SSHClient ssh = new SSHClient();
		SSHClientVO po = null;
		try {
			po = new SSHClientVO();
			ssh.addHostKeyVerifier(new PromiscuousVerifier());

			ssh.connect(host.getHostIp(), host.getHostPort());
			ssh.authPassword(host.getAccount(), host.getPassword());

			boolean isAuth = ssh.isAuthenticated();
			boolean isConn = ssh.isConnected();
			//logger.info("isAuthenticated={} ,isConnected={} ", isAuth, isConn);
			po.setAuth(isAuth);
			po.setConn(isConn);

			String localAddress = ssh.getLocalAddress().getHostAddress();
			String remoteAddress = ssh.getRemoteAddress().getHostAddress();
			//logger.info("local Address={} ,remote Address={} ", localAddress, remoteAddress);
			po.setLocalAddress(localAddress);
			po.setRemoteAddress(remoteAddress);

			ssh.useCompression();

			// 将集群中的所有的机器的[hostname.pub]发送到CD主机()
			logger.info("local file={} ,remote file={} ", local, remote);
			ssh.newSCPFileTransfer().upload(local, remote);
			return po;
			
		} finally {
			ssh.close();
			ssh.disconnect();
		}
		
	}
	
	static public SSHClientVO download(ServerHost host,final String local,final String remote) throws IOException {
		
		SSHClient ssh = new SSHClient();
		SSHClientVO po = null;
		try {
			po = new SSHClientVO();
			ssh.addHostKeyVerifier(new PromiscuousVerifier());

			ssh.connect(host.getHostIp(), host.getHostPort());
			ssh.authPassword(host.getAccount(), host.getPassword());

			boolean isAuth = ssh.isAuthenticated();
			boolean isConn = ssh.isConnected();
			logger.info("isAuthenticated={} ,isConnected={} ", isAuth, isConn);
			po.setAuth(isAuth);
			po.setConn(isConn);

			String localAddress = ssh.getLocalAddress().getHostAddress();
			String remoteAddress = ssh.getRemoteAddress().getHostAddress();
			logger.info("local Address={} ,remote Address={} ", localAddress, remoteAddress);
			po.setLocalAddress(localAddress);
			po.setRemoteAddress(remoteAddress);

			ssh.useCompression();

			// 将集群中的所有的机器的[hostname.pub]发送到CD主机()
			logger.info("local file={} ,remote file={} ", local, remote);
			ssh.newSCPFileTransfer().download(remote, local);
			return po;
		} finally {
			ssh.close();
			ssh.disconnect();
		}
		
	}
	
}
